package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.*;
import com.indusfo.spc.pojo.*;
import com.indusfo.spc.service.BatchDeteService;
import com.indusfo.spc.service.BatchFlowService;
import com.indusfo.spc.vo.HintMessage;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.annotation.RequestScope;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @Author liub
 * @Date 2019/12/4 15:56
 * @Version 1.0
 */

@Service
public class BatchFlowServiceImpl implements BatchFlowService {

    /**
     * 声明日志的静态变量
     */
    private static final Logger logger = LoggerFactory.getLogger(BatchFlowServiceImpl.class);

    @Resource
    private BatchFlowMapper batchFlowMapper;

    @Resource
    private BatchMapper batchMapper;

    @Resource
    private BatchStationMapper batchStationMapper;

    @Resource
    private BatchDeteMapper batchDeteMapper;

    @Resource
    private BatchBomMapper batchBomMapper;

    @Resource
    private BatchBadphenoMapper batchBadphenoMapper;

    @Resource
    private BatchRepairMapper batchRepairMapper;

    @Resource
    private BatchRepairFlowMapper batchRepairFlowMapper;

    @Resource
    private BatchRepairDeteMapper batchRepairDeteMapper;


    @Resource
    private OrderFlowMapper orderFlowMapper;

    @Resource
    private OrderStationMapper orderStationMapper;

    @Resource
    private OrderDeteMapper orderDeteMapper;

    @Resource
    private OrderBomMapper orderBomMapper;

    @Resource
    private OrderBadphenoMapper orderBadphenoMapper;


    @Resource
    private OrderRepairMapper orderRepairMapper;

    @Resource
    private OrderRepairFlowMapper orderRepairFlowMapper;

    @Resource
    private OrderRepairDeteMapper orderRepairDeteMapper;

    @Resource
    private RepairDetailMapper repairDetailMapper;

    /**
     * 查询
     */
    @Override
    public JSONObject queryBatchFlow(BatchFlow batchFlow) {
        try {
            //获取页面的数据个数
            Integer pagesize = batchFlow.getPagesize();
            //获取查询的页数
            Integer pageindex = batchFlow.getPageindex();
            if (pagesize != null && pageindex != null) {
                batchFlow.setIncept(pagesize * (pageindex - 1));
            }
            //执行查询操作
            List<BatchFlow> batchFlowList = batchFlowMapper.queryBatchFlow(batchFlow);

            //如果页数和页面数量都不为空的情况下进行计数
            if (batchFlowList.isEmpty()) {
                return JSONObject.oK(HintMessage.QUERY_NULL, batchFlowList, 0);
            } else {
                Integer count = batchFlowMapper.countBatchFlow(batchFlow);
                return JSONObject.oK(HintMessage.QUERY_SUCCESS, batchFlowList, count);
            }

        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());

        }
    }

    /**
     * 新增
     */
    @Override
    public JSONObject insertBatchFlow(BatchFlow batchFlow) {
        try {
            if (batchFlow.getBatchFlowId() != null) {
                throw new ModifyFailedException(HintMessage.INSERT_ID);
            }
            //初始化基础数据
            if (batchFlow.getDataState() == null) {
                batchFlow.setDataState(1);
            }
            if (batchFlow.getUserId().equals(0)) {
                batchFlow.setUserId(null);
            }
            if (batchFlow.getClasseId().equals(0)) {
                batchFlow.setClasseId(null);
            }
            if (batchFlow.getStationId().equals(0)) {
                batchFlow.setStationId(null);
            }
            if (batchFlow.getStartDate().equals(0)) {
                batchFlow.setDataState(null);
            }
            if (batchFlow.getEndDate().equals(0)) {
                batchFlow.setDataState(null);
            }
            //判重
            checkParam(batchFlow);
            //根据ID得到编号
            batchFlow.setBatchCode(batchMapper.selectById(batchFlow.getBatchId()));
            //执行新增操作
            Integer row = batchFlowMapper.insertBatchFlow(batchFlow);
            if (row == 0) {
                throw new ModifyFailedException(HintMessage.INSERT_DEFEATED);
            }
            return JSONObject.oK(HintMessage.INSERT_SUCCESS);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }

    }

    /**
     * 修改
     */
    @Override
    public JSONObject updateBatchFlow(BatchFlow batchFlow) {
        try {
            if (batchFlow.getBatchFlowId() == null) {
                throw new ParamsErrorException(HintMessage.UPDATE_ID);
            }
            //初始化基础数据
            if (batchFlow.getDataState() == null) {
                batchFlow.setDataState(1);
            }
            if (batchFlow.getUserId().equals(0)) {
                batchFlow.setUserId(null);
            }
            if (batchFlow.getClasseId().equals(0)) {
                batchFlow.setClasseId(null);
            }
            if (batchFlow.getStationId().equals(0)) {
                batchFlow.setStationId(null);
            }
            if (batchFlow.getStartDate().equals(0)) {
                batchFlow.setDataState(null);
            }
            if (batchFlow.getEndDate().equals(0)) {
                batchFlow.setDataState(null);
            }
            //判重
            checkParam(batchFlow);
            //根据ID得到编号
            batchFlow.setBatchCode(batchMapper.selectById(batchFlow.getBatchId()));
            //执行修改操作
            int row = batchFlowMapper.updateBatchFlow(batchFlow);
            if (row == 0) {
                throw new ModifyFailedException(HintMessage.UPDATE_DEFEATED);
            }
            return JSONObject.oK(HintMessage.UPDATE_SUCCESS);
        } catch (GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 删除
     */
    @Override
    public JSONObject deleteBatchFlow(List<Integer> batchFlowIds, Integer dataState) {
        try {
            //空值判断
            if (dataState == null) {
                throw new ParamsErrorException(HintMessage.DATASTATE_NULL);
            }
            if (batchFlowIds == null) {
                throw new ParamsErrorException(HintMessage.LIST_NULL);
            }
            //记录异常信息
            String msg = "";
            switch (dataState) {
                case 1:
                    msg = HintMessage.START_VALUE;
                    break;
                case 2:
                    msg = HintMessage.DELETE_VALUE;
                    break;
                case 3:
                    msg = HintMessage.STOP_VALUE;
                    break;
                default:
                    throw new ParamsErrorException(HintMessage.VIOLATION_OPERATION);
            }
            //执行删除操作
            int row = batchFlowMapper.deleteBatchFlow(batchFlowIds, dataState);
            //声明msg字符串，用来存抛出的异常
            if (row == 0) {
                msg = msg + HintMessage.DEFEATED_VALUE;
            } else {
                msg = msg + HintMessage.SUCCESS_VALUE;
            }
            return JSONObject.oK(msg);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 级联删除工序流程关联的工位、采集参数、关键物料、不良现象
     */
    @Transactional
    @Override
    public JSONObject deleteCascadeBatchFlow(List<Integer> batchFlowIds, Integer dataState) {
        //空值判断
        if (dataState == null) {
            throw new ParamsErrorException(HintMessage.DATASTATE_NULL);
        }
        if (batchFlowIds == null) {
            throw new ParamsErrorException(HintMessage.LIST_NULL);
        }
        //记录异常信息
        String msg = "";
        switch (dataState) {
            case 1:
                msg = HintMessage.START_VALUE;
                break;
            case 2:
                msg = HintMessage.DELETE_VALUE;
                break;
            case 3:
                msg = HintMessage.STOP_VALUE;
                break;
            default:
                throw new ParamsErrorException(HintMessage.VIOLATION_OPERATION);
        }
        //执行删除操作
        int row = batchFlowMapper.deleteBatchFlow(batchFlowIds, dataState);

        //根据传入的orderFlowIds查询对应的工位、采集参数、关键物料、不良现象集合
        for (Integer batchFlowId : batchFlowIds) {

            //级联执行工位删除
            List<BatchStation> batchStationList = batchStationMapper.queryBatchStationByBatchFlowId(batchFlowId);
            if (batchStationList != null && !batchStationList.isEmpty()) {
                for (BatchStation batchStation : batchStationList) {
                    batchStationMapper.deleteBatchStationByBatchStationId(batchStation.getBatchStationId(), dataState);
                }
            }

            //级联执行采集参数删除
            List<BatchDete> batchDeteList = batchDeteMapper.queryBatchDeteByBatchFlowId(batchFlowId);
            if (batchDeteList != null && !batchDeteList.isEmpty()) {
                for (BatchDete batchDete : batchDeteList) {
                    batchDeteMapper.deleteBatchDeteByBatchDeteId(batchDete.getBatchDeteId(), dataState);
                }
            }

            //级联执行关键物料删除
            List<BatchBom> batchBomList = batchBomMapper.queryBatchBomByBatchFlowId(batchFlowId);
            if (batchBomList != null && !batchBomList.isEmpty()) {
                for (BatchBom batchBom : batchBomList) {
                    batchBomMapper.deleteBatchBomByBatchBomId(batchBom.getBatchBomId(), dataState);
                }
            }

            //级联执行不良现象删除
            List<BatchBadpheno> batchBadphenoList = batchBadphenoMapper.queryBatchBadphenoByBatchFlowId(batchFlowId);
            if (batchBadphenoList != null && !batchBadphenoList.isEmpty()) {
                for (BatchBadpheno batchBadpheno : batchBadphenoList) {
                    batchBadphenoMapper.deleteBatchBadphenoByBatchBadphenoId(batchBadpheno.getBatchBadphenoId(), dataState);
                }
            }
        }

        //声明msg字符串，用来存抛出的异常
        if (row == 0) {
            msg = msg + HintMessage.DEFEATED_VALUE;
        } else {
            msg = msg + HintMessage.SUCCESS_VALUE;
        }
        return JSONObject.oK(msg);
    }

    /**
     * 判重
     */
    public void checkParam(BatchFlow batchFlow) {

        //批次ID
        Integer batchId = batchFlow.getBatchId();
        //工序ID
        Integer proId = batchFlow.getProId();
        //并序状态
        Integer sequence = batchFlow.getSequence();
        //顺序号
        Integer serialNum = batchFlow.getSerialNum();

        //批次ID
        if (batchId == null) {
            throw new ParamsErrorException(HintMessage.BATCHID_NULL);
        }
        //工序ID
        if (proId == null) {
            throw new ParamsErrorException(HintMessage.PROID_NULL);
        }
        //并序状态
        if (sequence == null) {
            throw new ParamsErrorException(HintMessage.SEQUENCE_NULL);
        }
        //顺序号
        if (serialNum == null) {
            throw new ParamsErrorException(HintMessage.SERIALNUM_NULL);
        }

        // 执行判重操作
        Integer Counts = batchFlowMapper.checkBatchFlow(batchFlow);
        if (Counts != 0) {
            throw new ParamsErrorException(HintMessage.REPEATING_DATA);
        }

    }

    /**
     * 保存流程工位
     */
    @Override
    public JSONObject saveBatchStation(BatchFlow batchFlow) {
        try {
            //判空
            if (batchFlow == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (batchFlow.getBatchFlowId() == null) {
                throw new ParamsErrorException(HintMessage.BATCHFLOWID_NULL);
            }

            //获取流程id
            Integer batchFlowId = batchFlow.getBatchFlowId();
            //获取list集合
            List<BatchStation> batchStationList = new ArrayList<>();
            batchStationList = batchFlow.getBatchStationList();
            for (BatchStation batchStation : batchStationList) {
                batchStation.setBatchFlowId(batchFlowId);
            }


            //获取数据库数据
            BatchStation batchStation1 = new BatchStation();
            batchStation1.setBatchFlowId(batchFlowId);
            List<BatchStation> oldbatchStationList = batchStationMapper.queryBatchStation(batchStation1);
            //删除
            List<Integer> deleteList = new ArrayList<>();
            List<Integer> oldIds = new ArrayList<>();
            List<Integer> newIds = new ArrayList<>();
            if (oldbatchStationList != null) {
                for (BatchStation batchStation : oldbatchStationList
                ) {
                    oldIds.add(batchStation.getBatchStationId());
                }
            }
            if (batchStationList != null) {
                for (BatchStation batchStation : batchStationList
                ) {
                    newIds.add(batchStation.getBatchStationId());
                }
            }
            HashSet h1 = new HashSet(oldIds);
            HashSet h2 = new HashSet(newIds);
            h1.removeAll(h2);
            deleteList.addAll(h1);
            int deleterow = 0;
            int insertrow = 0;
            int updaterow = 0;
            if (deleteList.size() != 0) {
                deleterow = batchStationMapper.deleteBatchStation(deleteList, 2);
            }
            //删除完成  进行新增 和 修改
            if (batchStationList != null) {
                for (BatchStation batchStation : batchStationList
                ) {
                    if (batchStation.getDataState() == null) {
                        batchStation.setDataState(1);
                    }

                    if (batchStation.getBatchStationId() != null) {
                        if (batchStation.getBatchStationId().equals(0)) {
                            batchStation.setBatchStationId(null);
                            // 执行判重操作
                            Integer Counts = batchStationMapper.checkBatchStation(batchStation);
                            if (Counts == 0) {
                                insertrow = insertrow + batchStationMapper.insertBatchStation(batchStation);
                            }

                        } else {
                            // 执行判重操作
                            Integer Counts = batchStationMapper.checkBatchStation(batchStation);
                            if (Counts == 0) {
                                updaterow = updaterow + batchStationMapper.updateBatchStation(batchStation);
                            }
                        }
                    }
                }
            }
            return JSONObject.oK(HintMessage.SAVE_SUCCESS, deleterow + insertrow + updaterow);
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 保存流程采集参数
     */
    @Override
    public JSONObject saveBatchDete(BatchFlow batchFlow) {
        try {
            //判空
            if (batchFlow == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (batchFlow.getBatchFlowId() == null) {
                throw new ParamsErrorException(HintMessage.BATCHFLOWID_NULL);
            }
            //获取流程id
            Integer batchFlowId = batchFlow.getBatchFlowId();
            //获取list集合
            List<BatchDete> batchDeteList = new ArrayList<>();
            batchDeteList = batchFlow.getBatchDeteList();
            for (BatchDete batchDete : batchDeteList) {
                batchDete.setBatchFlowId(batchFlowId);
            }


            //获取数据库数据
            BatchDete batchDete1 = new BatchDete();
            batchDete1.setBatchFlowId(batchFlowId);
            List<BatchDete> oldbatchDeteList = batchDeteMapper.queryBatchDete(batchDete1);
            //删除
            List<Integer> deleteList = new ArrayList<>();
            List<Integer> oldIds = new ArrayList<>();
            List<Integer> newIds = new ArrayList<>();
            if (oldbatchDeteList != null) {
                for (BatchDete batchDete : oldbatchDeteList
                ) {
                    oldIds.add(batchDete.getBatchDeteId());
                }
            }
            if (batchDeteList != null) {
                for (BatchDete batchDete : batchDeteList
                ) {
                    newIds.add(batchDete.getBatchDeteId());
                }
            }
            HashSet h1 = new HashSet(oldIds);
            HashSet h2 = new HashSet(newIds);
            h1.removeAll(h2);
            deleteList.addAll(h1);
            int deleterow = 0;
            int insertrow = 0;
            int updaterow = 0;
            if (deleteList.size() != 0) {
                deleterow = batchDeteMapper.deleteBatchDete(deleteList, 2);
            }
            //删除完成  进行新增 和 修改
            if (batchDeteList != null) {
                for (BatchDete batchDete : batchDeteList
                ) {
                    if (batchDete.getDataState() == null) {
                        batchDete.setDataState(1);
                    }


                    if (batchDete.getBatchDeteId() != null) {
                        if (batchDete.getBatchDeteId().equals(0)) {
                            batchDete.setBatchDeteId(null);
                            // 执行判重操作
                            Integer Counts = batchDeteMapper.checkBatchDete(batchDete);
                            if (Counts == 0) {
                                insertrow = insertrow + batchDeteMapper.insertBatchDete(batchDete);
                            }
                        } else {
                            // 执行判重操作
                            Integer Counts = batchDeteMapper.checkBatchDete(batchDete);
                            if (Counts == 0) {
                                updaterow = updaterow + batchDeteMapper.updateBatchDete(batchDete);
                            }
                        }
                    }
                }
            }
            return JSONObject.oK(HintMessage.SAVE_SUCCESS, deleterow + insertrow + updaterow);
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 保存流程物料
     */
    @Override
    public JSONObject saveBatchBom(BatchFlow batchFlow) {
        try {
            //判空
            if (batchFlow == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (batchFlow.getBatchFlowId() == null) {
                throw new ParamsErrorException(HintMessage.BATCHFLOWID_NULL);
            }
            //获取流程id
            Integer batchFlowId = batchFlow.getBatchFlowId();
            //获取list集合
            List<BatchBom> batchBomList = new ArrayList<>();
            batchBomList = batchFlow.getBatchBomList();
            for (BatchBom batchBom : batchBomList) {
                batchBom.setBatchFlowId(batchFlowId);
            }


            //获取数据库数据
            BatchBom batchBom1 = new BatchBom();
            batchBom1.setBatchFlowId(batchFlowId);
            List<BatchBom> oldbatchBomList = batchBomMapper.queryBatchBom(batchBom1);
            //删除
            List<Integer> deleteList = new ArrayList<>();
            List<Integer> oldIds = new ArrayList<>();
            List<Integer> newIds = new ArrayList<>();
            if (oldbatchBomList != null) {
                for (BatchBom batchBom : oldbatchBomList
                ) {
                    oldIds.add(batchBom.getBatchBomId());
                }
            }
            if (batchBomList != null) {
                for (BatchBom batchBom : batchBomList
                ) {
                    newIds.add(batchBom.getBatchBomId());
                }
            }
            HashSet h1 = new HashSet(oldIds);
            HashSet h2 = new HashSet(newIds);
            h1.removeAll(h2);
            deleteList.addAll(h1);
            int deleterow = 0;
            int insertrow = 0;
            int updaterow = 0;
            if (deleteList.size() != 0) {
                deleterow = batchBomMapper.deleteBatchBom(deleteList, 2);
            }
            //删除完成  进行新增 和 修改
            if (batchBomList != null) {
                for (BatchBom batchBom : batchBomList
                ) {
                    if (batchBom.getDataState() == null) {
                        batchBom.setDataState(1);
                    }


                    if (batchBom.getBatchBomId() != null) {
                        if (batchBom.getBatchBomId().equals(0)) {
                            batchBom.setBatchBomId(null);
                            // 执行判重操作
                            Integer Counts = batchBomMapper.checkBatchBom(batchBom);
                            if (Counts == 0) {
                                insertrow = insertrow + batchBomMapper.insertBatchBom(batchBom);
                            }

                        } else {
                            // 执行判重操作
                            Integer Counts = batchBomMapper.checkBatchBom(batchBom);
                            if (Counts == 0) {
                                updaterow = updaterow + batchBomMapper.updateBatchBom(batchBom);
                            }
                        }
                    }
                }
            }
            return JSONObject.oK(HintMessage.SAVE_SUCCESS, deleterow + insertrow + updaterow);
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 保存不良现象
     */
    @Override
    public JSONObject saveBatchBadpheno(BatchFlow batchFlow) {
        try {
            //判空
            if (batchFlow == null) {
                throw new ParamsErrorException(HintMessage.DATA_NULL);
            }
            if (batchFlow.getBatchFlowId() == null) {
                throw new ParamsErrorException(HintMessage.BATCHFLOWID_NULL);
            }
            //获取流程id
            Integer batchFlowId = batchFlow.getBatchFlowId();

            //获取list集合
            List<BatchBadpheno> batchBadphenoList = new ArrayList<>();
            batchBadphenoList = batchFlow.getBatchBadphenoList();
            for (BatchBadpheno batchBadpheno : batchBadphenoList) {
                batchBadpheno.setBatchFlowId(batchFlowId);
            }

            //获取数据库数据
            BatchBadpheno batchBadpheno1 = new BatchBadpheno();
            batchBadpheno1.setBatchFlowId(batchFlowId);
            List<BatchBadpheno> oldbatchBadphenoList = batchBadphenoMapper.queryBatchBadpheno(batchBadpheno1);
            //删除
            List<Integer> deleteList = new ArrayList<>();
            List<Integer> oldIds = new ArrayList<>();
            List<Integer> newIds = new ArrayList<>();
            if (oldbatchBadphenoList != null) {
                for (BatchBadpheno batchBadpheno : oldbatchBadphenoList
                ) {
                    oldIds.add(batchBadpheno.getBatchBadphenoId());
                }
            }
            if (batchBadphenoList != null) {
                for (BatchBadpheno batchBadpheno : batchBadphenoList
                ) {
                    newIds.add(batchBadpheno.getBatchBadphenoId());
                }
            }
            HashSet h1 = new HashSet(oldIds);
            HashSet h2 = new HashSet(newIds);
            h1.removeAll(h2);
            deleteList.addAll(h1);
            int deleterow = 0;
            int insertrow = 0;
            int updaterow = 0;
            if (deleteList.size() != 0) {
                deleterow = batchBadphenoMapper.deleteBatchBadpheno(deleteList, 2);
            }
            //删除完成  进行新增 和 修改
            if (batchBadphenoList != null) {
                for (BatchBadpheno batchBadpheno : batchBadphenoList
                ) {
                    if (batchBadpheno.getDataState() == null) {
                        batchBadpheno.setDataState(1);
                    }


                    if (batchBadpheno.getBatchBadphenoId() != null) {
                        if (batchBadpheno.getBatchBadphenoId().equals(0)) {
                            batchBadpheno.setBatchBadphenoId(null);
                            // 执行判重操作
                            Integer Counts = batchBadphenoMapper.checkBatchBadpheno(batchBadpheno);
                            if (Counts == 0) {
                                insertrow = insertrow + batchBadphenoMapper.insertBatchBadpheno(batchBadpheno);
                            }

                        } else {
                            // 执行判重操作
                            Integer Counts = batchBadphenoMapper.checkBatchBadpheno(batchBadpheno);
                            if (Counts == 0) {
                                updaterow = updaterow + batchBadphenoMapper.updateBatchBadpheno(batchBadpheno);
                            }
                        }
                    }
                }
            }
            return JSONObject.oK(HintMessage.SAVE_SUCCESS, deleterow + insertrow + updaterow);
        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    @Override
    public JSONObject leadBatchFlow(Integer orderId, Integer batchId, String batchCode) {
        try {
            //判空
            if (orderId == null) {
                throw new ParamsErrorException(HintMessage.MATVERID_NULL);
            }
            if (batchId == null && batchCode == null) {
                throw new ParamsErrorException(HintMessage.ORDERID_NULL);
            } else {
                if (batchCode != null) {
                    Batch b = new Batch();
                    b.setBatchCode(batchCode);
                    List<Batch> batches = batchMapper.selectBatch(b);
                    if (batches != null) {
                        batchId = batches.get(0).getBatchId();
                    }
                }
            }
            //查询旧的流程
            OrderFlow orderFlow1 = new OrderFlow();
            orderFlow1.setOrderId(orderId);
            List<OrderFlow> orderFlowList = orderFlowMapper.queryOrderFlow(orderFlow1);
            for (OrderFlow orderFlow : orderFlowList) {
                //新增流程
                Integer orderFlowId = orderFlow.getOrderFlowId();
                BatchFlow batchFlow = new BatchFlow();
                batchFlow.setNextPro(orderFlow.getNextPro());
                batchFlow.setBatchFlowId(null);
                batchFlow.setBatchId(batchId);
                batchFlow.setProId(orderFlow.getProId());
                batchFlow.setSequence(orderFlow.getSequence());
                batchFlow.setSerialNum(orderFlow.getSerialNum());
                batchFlow.setRisePro(orderFlow.getRisePro());
                batchFlow.setDataState(1);
                batchFlowMapper.insertBatchFlow(batchFlow);
                Integer batchFlowId = batchFlow.getBatchFlowId();

                //新增工位
                OrderStation orderStation1 = new OrderStation();
                orderStation1.setOrderFlowId(orderFlowId);
                List<OrderStation> orderStationList = orderStationMapper.queryOrderStation(orderStation1);
                for (OrderStation orderStation : orderStationList) {
                    BatchStation batchStation = new BatchStation();
                    batchStation.setBatchFlowId(batchFlowId);
                    batchStation.setStationId(orderStation.getStationId());
                    batchStation.setDataState(1);
                    batchStationMapper.insertBatchStation(batchStation);
                }
                //新增不良
                OrderBadpheno orderBadpheno1 = new OrderBadpheno();
                orderBadpheno1.setOrderFlowId(orderFlowId);
                List<OrderBadpheno> orderBadphenoList = orderBadphenoMapper.queryOrderBadpheno(orderBadpheno1);
                for (OrderBadpheno orderBadpheno : orderBadphenoList) {
                    BatchBadpheno batchBadpheno = new BatchBadpheno();
                    batchBadpheno.setBatchFlowId(batchFlowId);
                    batchBadpheno.setBadphenoId(orderBadpheno.getBadphenoId());
                    batchBadpheno.setDataState(1);
                    batchBadphenoMapper.insertBatchBadpheno(batchBadpheno);
                }

                //新增关键物料
                OrderBom orderBom1 = new OrderBom();
                orderBom1.setOrderFlowId(orderFlowId);
                List<OrderBom> orderBomList = orderBomMapper.queryOrderBom(orderBom1);
                for (OrderBom orderBom : orderBomList) {
                    BatchBom batchBom = new BatchBom();
                    batchBom.setBatchFlowId(batchFlowId);
                    batchBom.setMatId(orderBom.getMatId());
                    batchBom.setReduceTypeId(orderBom.getReduceTypeId());
                    batchBom.setReduceNum(orderBom.getReduceNum());
                    batchBom.setDataState(1);
                    batchBomMapper.insertBatchBom(batchBom);
                }
                //新增采集项目
                OrderDete orderDete1 = new OrderDete();
                orderDete1.setOrderFlowId(orderFlowId);
                List<OrderDete> orderDeteList = orderDeteMapper.queryOrderDete(orderDete1);
                for (OrderDete orderDete : orderDeteList) {
                    BatchDete batchDete = new BatchDete();
                    batchDete.setBatchFlowId(batchFlowId);
                    batchDete.setDeteId(orderDete.getDeteId());
                    batchDete.setLowerLimit(orderDete.getLowerLimit());
                    batchDete.setMiddleLimit(orderDete.getMiddleLimit());
                    batchDete.setUpperLimit(orderDete.getUpperLimit());
                    batchDete.setDataState(1);
                    batchDeteMapper.insertBatchDete(batchDete);
                }


            }

            //查询旧的维修流程
            OrderRepair orderRepair1 = new OrderRepair();
            orderRepair1.setOrderId(orderId);
            List<OrderRepair> orderRepairList = orderRepairMapper.queryOrderRepair(orderRepair1);
            for (OrderRepair orderRepair : orderRepairList) {
                Integer orderRepairId = orderRepair.getOrderRepairId();
                BatchRepair batchRepair = new BatchRepair();
                batchRepair.setBatchId(batchId);
                batchRepair.setRepairId(orderRepair.getRepairId());
                batchRepair.setDataState(1);
                batchRepairMapper.insertBatchRepair(batchRepair);
                Integer batchRepairId = batchRepair.getBatchRepairId();

                //新增工序
                OrderRepairFlow orderRepairFlow1 = new OrderRepairFlow();
                orderRepairFlow1.setOrderRepairId(orderRepairId);
                List<OrderRepairFlow> orderRepairFlowList = orderRepairFlowMapper.queryOrderRepairFlow(orderRepairFlow1);
                for (OrderRepairFlow orderRepairFlow : orderRepairFlowList) {
                    //查询流程id
                    BatchFlow batchFlow = new BatchFlow();
                    batchFlow.setBatchId(batchId);
                    List<BatchFlow> batchFlowList = batchFlowMapper.queryBatchFlow(batchFlow);
                    for (BatchFlow batchFlow1 : batchFlowList) {
                        if (batchFlow.getProId() != null && batchFlow.getProId() != null) {
                            if (batchFlow.getProId().equals(orderRepairFlow.getProId()) && batchFlow.getSerialNum().equals(orderRepairFlow.getSerialNum())) {
                                BatchRepairFlow batchRepairFlow = new BatchRepairFlow();
                                batchRepairFlow.setBatchFlowId(batchFlow1.getBatchFlowId());
                                batchRepairFlow.setBatchRepairId(batchRepairId);
                                batchRepairFlow.setBatchRepairFlowId(null);
                                batchRepairFlow.setDataState(1);
                                batchRepairFlowMapper.insertBatchRepairFlow(batchRepairFlow);

                            }

                        }
                    }


                }

                //新增采集项目
                RepairDetail repairDetail = new RepairDetail();
                repairDetail.setRepairId(orderRepair1.getRepairId());
                List<RepairDetail> repairDetailList = repairDetailMapper.selectAll(repairDetail);
                for (RepairDetail repairDetail1 : repairDetailList) {
                    OrderRepairDete orderRepairDete = new OrderRepairDete();
                    orderRepairDete.setOrderRepairId(orderRepairId);
                    orderRepairDete.setRepairDetailId(repairDetail1.getRepairDetailId());
                    List<OrderRepairDete> orderRepairDeteList = orderRepairDeteMapper.queryOrderRepairDete(orderRepairDete);
                    for (OrderRepairDete orderRepairDete1 : orderRepairDeteList) {
                        BatchRepairDete batchRepairDete = new BatchRepairDete();
                        batchRepairDete.setBatchRepairId(batchRepairId);
                        batchRepairDete.setRepairDetailId(orderRepairDete1.getRepairDetailId());
                        batchRepairDete.setLowerLimit(orderRepairDete1.getLowerLimit());
                        batchRepairDete.setMiddleLimit(orderRepairDete1.getMiddleLimit());
                        batchRepairDete.setUpperLimit(orderRepairDete1.getUpperLimit());
                        batchRepairDete.setDataState(1);
                        batchRepairDete.setBatchRepairDeteId(null);
                        batchRepairDeteMapper.insertBatchRepairDete(batchRepairDete);
                    }

                }


            }
            return JSONObject.oK(HintMessage.SAVE_SUCCESS, batchId);

        } catch (GlobalException e) {// 捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 批次流程维修下工序新增查询
     */
    @Override
    public JSONObject queryListBatchFlowToInsert(BatchFlow batchFlow) {
        try {
            //获取页面的数据个数
            Integer pagesize = batchFlow.getPagesize();
            //获取查询的页数
            Integer pageindex = batchFlow.getPageindex();
            if (pagesize != null && pageindex != null) {
                batchFlow.setIncept(pagesize * (pageindex - 1));
            }
            //执行查询操作
            List<BatchFlow> batchFlowList = batchFlowMapper.queryBatchFlowToInsert(batchFlow);

            //如果页数和页面数量都不为空的情况下进行计数
            if (batchFlowList.isEmpty()) {
                return JSONObject.oK(HintMessage.QUERY_NULL, batchFlowList, 0);
            } else {
                Integer count = batchFlowMapper.countBatchFlowToInsert(batchFlow);
                return JSONObject.oK(HintMessage.QUERY_SUCCESS, batchFlowList, count);
            }

        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());

        }
    }
}
